package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.po.User;
import com.jeff.service.UserService;
import com.jeff.util.MD5Encryption;
import com.jeff.util.ResultMap;
import com.jeff.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色权限 1 可创建 2 3 4
 * 2 可创建 3,4
 * 3 可创建4
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    public UserService userService;

    @RequestMapping("add")
    @ResponseBody
    public ResultMap add(HttpServletRequest request, User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setState(mConst.STATE_T);
            String userGradeId = BaseInfo.GetUserGradeId(request);
            try{
                gradeCheck(userGradeId, user.getGradeId());
            }catch (Error e){
                return rm.fail().info("增加失败," + e.getMessage());
            }
            //todo departMentCheck

            /**
             * check start
             */
            User check = new User();
            check.setState(mConst.STATE_T);
            check.setId(null);
            check.setLoginName(user.getLoginName());
            List<User> checkList = userService.list(check);
            if (checkList != null && checkList.size() != 0) {
                return rm.fail().info("增加失败,有重名账号");
            }
            user.setPassword(MD5Encryption.getMD5(user.getPassword()));
            userService.add(user);
            rm.success().info("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("增加失败");
        }
        return rm;
    }

    private void gradeCheck(String gradeId, String targetGradeId) {
        if (StringUtil.isEmpty(targetGradeId) || StringUtil.isEmpty(gradeId)) {
            throw new Error("未设置角色");
        }
        if (gradeId.equals(mConst.GRADE_TEACHER)) {
            if (!"234".contains(targetGradeId)) {
                throw new Error("无权限添加该角色用户");
            }
        } else if (gradeId.equals(mConst.GRADE_MANAGER)) {
            if (!"34".contains(targetGradeId)) {
                throw new Error("无权限添加该角色用户");
            }
        } else if (gradeId.equals(mConst.GRADE_ASSISTANT)) {
            if (!targetGradeId.equals(mConst.GRADE_MEMBER)) {
                throw new Error("无权限添加该角色用户");
            }
        } else if (gradeId.equals(mConst.GRADE_MEMBER)) {
            throw new Error("无权限添加用户");
        }

    }

    @RequestMapping("reg")
    @ResponseBody
    public ResultMap reg(HttpServletRequest request, User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setState(mConst.STATE_T);
            user.setCreateIp(BaseInfo.GetIp(request));
            userService.add(user);
            rm.success().info("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("增加失败");
        }
        return rm;
    }

    @RequestMapping("list")
    @ResponseBody
    public ResultMap list() {
        ResultMap rm = new ResultMap();
        try {
            User user = new User();
            user.setId(null);
            user.setState(mConst.STATE_T);
            List<User> list = userService.list(user);
            rm.success().data(list);
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("page")
    @ResponseBody
    public ResultMap page(HttpServletRequest request, User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setId(null);
            user.setState(mConst.STATE_T);
            user.setDeptId(BaseInfo.GetUserDeptId(request));
            rm.success().page(userService.findByPage(user));
        /*	if (BaseInfo.GetUserGradeId(request).equals("2")) {
                user.setGradeId("3");
				Page<UserDept> list = userService.findWithDeptByPage(user);
				rm.success().page(list);
			} else if (BaseInfo.GetUserGradeId(request).equals("1")) {
				user.setGradeId("3");
				Page<UserDept> list = userService.findWithDeptParentIdByPage(user);
				rm.success().page(list);
			} else if (BaseInfo.GetUserGradeId(request).equals("4")){
				user.setDeptId(null);
				Page<UserDept> list = userService.findWithDeptByPage(user);
				rm.success().page(list);
			}*/

        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }

    @RequestMapping("getUserInfo")
    @ResponseBody
    public ResultMap getUserInfo(HttpServletRequest request) {
        ResultMap rm = new ResultMap();
        try {
            User us = userService.getById(BaseInfo.GetUserId(request));
            rm.success().data(us);
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;

    }

    /**
     * 更新本人的信息
     *
     * @param request
     * @param user
     * @return
     */
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public ResultMap updateUserInfo(HttpServletRequest request, User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setId(BaseInfo.GetUserId(request));
            rm.success().data(userService.update(user));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("更新失败");
        }
        return rm;
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultMap update(User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setLoginName(null);
    /*		if(!StringUtil.isEmpty(user.getCard())){
				user.setLoginName(user.getCard());
				User check = new User();
				check.setState(mConst.STATE_T);
				check.setId(null);
				check.setLoginName(user.getLoginName());
				List<User> checkList = userService.list(check);
				if(checkList!=null&&checkList.size()!=0){
					if(checkList.size()==1){
						//是本人，本人重复传了相同的card
						if(!checkList.get(0).getId().equals(user.getId())){
							return rm.fail().info("修改失败,有重名账号");
						}

					}else{
						return rm.fail().info("修改失败,有重名账号");
					}
				}
			}*/

            userService.update(user);
            rm.success().info("修改成功");
        } catch (Exception e) {
            rm.fail().info("修改失败");
        }
        return rm;
    }
	
	

/*	@RequestMapping("check")
	@ResponseBody
	public Boolean check(User user) {
		try {
			User us = userService.check(user);
			if (us != null) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		return true;

	}*/

    @RequestMapping("remove")
    @ResponseBody
    public ResultMap remove(User user) {
        ResultMap rm = new ResultMap();
        try {
            user.setState(mConst.STATE_F);
            userService.update(user);
            rm.success().info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("删除失败");
        }
        return rm;
    }


}
